<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import DefaultLayout from '@/components/layout/DefaultLayout.vue'
import { useBlogStore } from '@/store/blog'

const router = useRouter()
const blogStore = useBlogStore()

const blogs = ref([])
const loading = ref(true)
const sortType = ref('latest') // 默认按最新排序

// 排序选项
const sortOptions = [
  { value: 'latest', label: '最新发布' },
  { value: 'views', label: '最多浏览' },
  { value: 'likes', label: '最多点赞' }
]

// 获取博客列表
const fetchBlogs = async () => {
  loading.value = true
  try {
    const data = await blogStore.fetchTopBlogs(sortType.value)
    blogs.value = data.map(blog => ({
      ...blog,
      typeName: blog.blogType?.typeName || '未分类',
      authorName: blog.user?.name || '未知作者'
    }))
  } catch (error) {
    console.error('获取博客列表失败:', error)
  } finally {
    loading.value = false
  }
}

// 监听排序方式变化
const handleSortChange = () => {
  fetchBlogs()
}

// 跳转到博客详情
const goToBlogDetail = (id) => {
  console.log('从首页跳转到博客详情，ID:', id)
  console.log('目标路径:', `/blogs/${id}`)
  router.push(`/blogs/${id}`)
}

onMounted(() => {
  fetchBlogs()
})
</script>

<template>
  <DefaultLayout>
    <div class="home-page">
      <!-- 页面标题和排序选择 -->
      <div class="page-header">
        <h1 class="page-title">推荐阅读</h1>
        <div class="sort-selector">
          <label>排序方式：</label>
          <select v-model="sortType" @change="handleSortChange">
            <option v-for="option in sortOptions" :key="option.value" :value="option.value">
              {{ option.label }}
            </option>
          </select>
        </div>
      </div>

      <!-- 博客列表 -->
      <div v-if="loading" class="loading-container">
        <div class="loading-spinner"></div>
      </div>

      <div v-else-if="blogs.length === 0" class="empty-state">
        <div class="empty-icon">📝</div>
        <h3>暂无博客</h3>
        <p>敬请期待更多精彩内容</p>
      </div>

      <div v-else class="blogs-grid">
        <div 
          v-for="blog in blogs" 
          :key="blog.blogId" 
          class="blog-card"
          @click="goToBlogDetail(blog.blogId)"
        >
          <div class="blog-image" :style="blog.coverImage ? `background-image: url(${blog.coverImage})` : ''">
            <span v-if="!blog.coverImage" class="no-image">博客</span>
          </div>
          <div class="blog-content">
            <div class="blog-meta">
              <span class="blog-type">{{ blog.typeName }}</span>
              <span class="blog-date">{{ new Date(blog.createTime).toLocaleDateString() }}</span>
            </div>
            <h3 class="blog-title">{{ blog.title }}</h3>
            <p class="blog-summary">{{ blog.summary || (blog.content ? blog.content.replace(/<[^>]+>/g, '').substring(0, 120) + '...' : '') }}</p>
            <div class="blog-footer">
              <div class="blog-author">{{ blog.authorName }}</div>
              <div class="blog-stats">
                <span class="blog-views">{{ blog.views || 0 }} 阅读</span>
                <span class="blog-likes">{{ blog.likes || 0 }} 赞</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </DefaultLayout>
</template>

<style scoped>
.home-page {
  padding: 2rem 0;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2rem;
}

.page-title {
  font-size: 2rem;
  font-weight: 600;
  margin: 0;
  color: var(--text-color);
}

.sort-selector {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.sort-selector label {
  color: var(--text-secondary);
}

.sort-selector select {
  padding: 0.5rem;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  background-color: var(--background);
  color: var(--text-color);
  font-size: 0.95rem;
  cursor: pointer;
}

.blogs-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 2rem;
}

.blog-card {
  border-radius: 12px;
  overflow: hidden;
  background: var(--background-dark);
  box-shadow: var(--card-shadow);
  transition: transform 0.3s, box-shadow 0.3s;
  cursor: pointer;
}

.blog-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}

.blog-image {
  height: 160px;
  background-color: var(--background-darker);
  background-size: cover;
  background-position: center;
  display: flex;
  align-items: center;
  justify-content: center;
}

.no-image {
  color: var(--text-light);
  font-size: 1.5rem;
  font-weight: 500;
}

.blog-content {
  padding: 1.5rem;
}

.blog-meta {
  display: flex;
  justify-content: space-between;
  margin-bottom: 1rem;
  font-size: 0.875rem;
}

.blog-type {
  color: var(--primary-color);
  font-weight: 600;
}

.blog-date {
  color: var(--text-light);
}

.blog-title {
  font-size: 1.25rem;
  margin-bottom: 0.75rem;
  line-height: 1.4;
  color: var(--text-color);
}

.blog-summary {
  font-size: 0.95rem;
  line-height: 1.6;
  color: var(--text-light);
  margin-bottom: 1.5rem;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.blog-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 1rem;
  border-top: 1px solid var(--border-color);
}

.blog-author {
  font-weight: 500;
  color: var(--text-color);
}

.blog-stats {
  display: flex;
  gap: 1rem;
  color: var(--text-light);
  font-size: 0.875rem;
}

.empty-state {
  text-align: center;
  padding: 4rem 0;
}

.empty-icon {
  font-size: 3rem;
  margin-bottom: 1rem;
}

.empty-state h3 {
  font-size: 1.5rem;
  margin-bottom: 0.5rem;
  color: var(--text-color);
}

.empty-state p {
  color: var(--text-light);
}

.loading-container {
  display: flex;
  justify-content: center;
  padding: 3rem 0;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid rgba(74, 111, 165, 0.2);
  border-radius: 50%;
  border-top-color: var(--primary-color);
  animation: spin 1s linear infinite;
}

@keyframes spin {
  100% {
    transform: rotate(360deg);
  }
}

@media (max-width: 768px) {
  .page-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 1rem;
  }
  
  .page-title {
    font-size: 1.75rem;
  }
  
  .blogs-grid {
    grid-template-columns: 1fr;
  }
}
</style> 